package com.vehicle.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.vehicle.bean.DataGridView;
import com.vehicle.bean.UserVo;
import com.vehicle.mapper.UserMapper;
import com.vehicle.pojo.User;
import com.vehicle.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Set;

@Service
@Transactional
public class UserServiceImp implements UserService {
    private Logger logger=Logger.getLogger(this.getClass());

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findUserByUid(String uname,String password) {
       User user= userMapper.selectUserByTelephone(uname);
       if (user.getPassword()!=null&&user.getPassword().equals(password)){
           return user;
       }else{
           return  null;
        }

    }
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    @Override
    public DataGridView findUsersBypage(UserVo userVo) {
        Page<Object> page= PageHelper.startPage(userVo.getPage(),userVo.getLimit());
        List<User> users = userMapper.selectUsersByPage(userVo);
        DataGridView dataGridView=new DataGridView();
        dataGridView.setCount(page.getTotal());
        dataGridView.setData(users);
        return dataGridView;
    }
    @Transactional
    @Override
    public void addUser(User user)  throws RuntimeException{
        User u=userMapper.selectUserByTelephone(user.getUname());
       if (u!=null){
           throw new RuntimeException("用户已存在！请重新添加！");
       }
        userMapper.insertUser(user);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void deleteUser(Integer uid) {
        userMapper.deleteUserRole(uid);
        userMapper.deleteUser(uid);
    }

    //修改用户
    @Override
    public void updateUser(User user) {

        userMapper.updateUser(user);
    }

    @Override
    public User findUserByTelephone(String uname) {
        return userMapper.selectUserByTelephone(uname);
    }

    //分配角色
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void assignRole(Integer uid, Integer[] rids) {
        userMapper.deleteUserRoleByUid(uid);
        if(rids!=null&&rids.length>0){
            for (Integer rid:rids) {
                userMapper.insertUserRole(uid,rid);
            }
        }
    }

    //认证
    @Override
    public User findUserByUname(String uname) {
        return userMapper.selectUserByTelephone(uname);
    }


    //授权
    @Override
    public Set<String> findRolesNameByUname(String uname) {
        return userMapper.selectRolesNameByUname(uname);
    }

    @Override
    public Set<String> findPercodesByUname(String uname) {
        return userMapper.selectPercodesByUname(uname);
    }


    //修改密码
    @Override
    public void changePwd(User user,String newPsw1,String newPsw2) {
        if (user!=null&&newPsw1.equals(newPsw2)){
           user.setPassword(newPsw1);
           userMapper.updatePwd(user);
        }
    }

    @Override
    public User findUserById(Integer uid) {
        return userMapper.selectUserById(uid);
    }
    //批量修改状态为3  假删除
    @Override
    public void updateAvailable( Integer[] uids) {
        for (Integer uid:uids
             ) {
            userMapper.updateStatusToThree(uid);
        }

    }
    //启用禁用
    @Override
    public void updateStatusOneZero(User user) {
        if (user.getAvailable()==1){
            userMapper.updateStatus(user.getUid());
        }else if (user.getAvailable()==0){
            userMapper.updateStatusToOne(user.getUid());
        }
    }
}
